<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\AdminInsertRequest;
use App\Http\Requests\Admin\AdminUpdateRequest;
use App\Models\Admin;
use Hash;
class AdminController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $count = $request -> input('count',10);
        $search = $request -> input('search','');
        $request = $request -> all();
        // 查询数据并分页
        $data = Admin::where('aname','like','%'.$search.'%')->where('asuper','0')->orderby('aname')->paginate($count);
        // 加载模版
        return view('admin.admin.index',['title'=>'管理员列表','data'=>$data,'request'=>$request]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //引入管理员添加模块
        return view('admin.admin.create',["title"=>"添加管理员"]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AdminInsertRequest $request)
    {
        //
        // 获取所有的值
        $data = $request -> all();
        // 处理插入
        $admin = new Admin;
        $admin->aname = $data['aname'];
        $admin->apassword = Hash::make($data['apassword']);
        $admin->remember_token = str_random(50); //str_random 随机字符串

        if($admin->save()){
            return redirect('/admin/admin')->with('success','添加成功');
        }else{
            return back()->with('error','添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $data = Admin::find($id);
        $data->astatus ? $data->astatus = 0 : $data->astatus = 1;
        if($data->save()){
            return redirect('/admin/admin')->with('success','操作成功');
        }else{
            return back()->with('error','操作失败');
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $data = Admin::find($id);
        return view("admin.admin.edit",['title'=>'修改管理员','data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(AdminUpdateRequest $request, $id)
    {
        //
        $oldpassword = $request->oldpassword;
        $data = Admin::find($id);
        if (Hash::check($oldpassword, $data->apassword)) {

            $data->apassword = Hash::make($request->apassword);
            if($data->save()){
                return redirect('/admin/admin')->with('success','修改成功');
            }else{
                return back()->with('error','修改失败');
            }
        }else{
            return back()->with('error','原密码错误');
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $data = [];
        $re =  Admin::find($id)->delete();
//         删除成功
        if($re){
             $data['status']= 0;
             $data['msg']='删除成功';
        }else{
            $data['status']= 1;
            $data['msg']='删除失败';
        }
        return $data;

    }
}
